package com.linkai.注解和反射.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @Description: 获得类的信息
 * @Author: 林凯
 * @Date: 2021/10/21 18:53
 */
public class Test08 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
        Class<?> c1 = Class.forName("com.linkai.注解和反射.reflect.User");

        User user = new User();
        c1 = user.getClass();

        // 获得类的名字
        System.out.println(c1.getName());
        // 获得类的简单名字
        System.out.println(c1.getSimpleName());
        System.out.println("====================");

        // 获得类的属性
        Field[] fields = c1.getFields();        // 只能找到public属性
        for (Field f:fields) {
            System.out.println(f);
        }

        // 找到全部的属性
        Field[] declaredFields = c1.getDeclaredFields();
        for (Field f:declaredFields) {
            System.out.println(f);
        }

        // 获得指定属性的值
        System.out.println("指定属性======");
        Field name = c1.getDeclaredField("name");
        System.out.println(name);

        // 获得类的方法
        System.out.println("获得本类及其父类的所有法");
        Method[] methods = c1.getMethods(); // 获得本类及其父类的所有法
        for (Method m:methods
             ) {
            System.out.println(m);
        }
        System.out.println("获得本类的方法");
        Method[] declaredMethods = c1.getDeclaredMethods();
        for (Method m:declaredMethods
             ) {
            System.out.println(m);
        }

        // 获得指定方法
        System.out.println("获得指定方法");
        Method getName = c1.getMethod("getName", null);
        Method setName = c1.getMethod("setName", String.class);
        System.out.println(getName);
        System.out.println(setName);

        // 获得指定的构造器
        System.out.println("=======");
        Constructor<?>[] constructors = c1.getConstructors();
        for (Constructor c:constructors
             ) {
            System.out.println(c);
        }

        // 获得指定的构造器
        Constructor<?> declaredConstructor = c1.getDeclaredConstructor(String.class, int.class, int.class);
        System.out.println(declaredConstructor);


    }
}
